<?php

namespace common\entities\myhtsq;

use common\entities\ContractEntity;
use Yii;
use Exception;
use common\error\ErrorCode;

/**
 * This is the model class for table "myscrm_invoice_summary".
 *
 * @property string $summary_id
 * @property string $invoice_id
 * @property string $summary_type
 * @property string $summary_amount
 * @property string $created_on
 * @property string $created_by
 * @property string $modified_on
 * @property string $modified_by
 */
class InvoiceSummaryEntity extends ContractEntity
{

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return 'myscrm_invoice_summary';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['created_on', 'modified_on'], 'safe'],
            [['summary_amount'], 'number'],
            [['summary_type'], 'string'],
            [['invoice_id', 'summary_id', 'created_by', 'modified_by'], 'string', 'max' => 36]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'summary_id' => '摘要ID',
            'invoice_id' => '发票ID',
            'summary_type' => '摘要类型',
            'summary_amount' => '摘要金额',
            'created_on' => '创建时间',
            'created_by' => '创建人',
            'modified_on' => '修改时间',
            'modified_by' => '修改人'
        ];
    }

    /**
     * 根据发票ID删除对应的发票摘要信息
     * @param string $invoiceId 发票ID
     * @return boolean 返回true时表示删除成功，否则失败
     */
     public static function deleteInvoiceSummaryByInvoiceId($invoiceId){
         try {
            self::deleteAll(['invoice_id' => $invoiceId]);
            return true;
        } catch (Exception $ex) {
            return self::setAndReturn(ErrorCode::ERR_INVOICE_SUMMARY_DELETE_FAIL, $ex->getMessage());
        }
    }
    
    /**
     * 根据发票ID获取发票信息
     * @param string $invoiceId
     * @return array
     */
    public static function getInvoiceSummarysByInvoiceId($invoiceId){
        $entities = self::findAll(['invoice_id' => $invoiceId]);
        return $entities;
    }
}
